package com.xunwulian.web.controller.pay;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xunwulian.base.dao.TransactionDao;
import com.xunwulian.base.entity.Page;
import com.xunwulian.base.entity.TransactionEntity;
import com.xunwulian.base.entity.User;
import com.xunwulian.base.util.PageData;
import com.xunwulian.web.controller.BaseController;
import com.xunwulian.web.entity.TransactionEntityWrapper;
import com.xunwulian.web.innerservice.InnerBillService;
import com.xunwulian.web.util.Const;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/transaction")
public class TransactionController extends BaseController {

    String menuUrl = "transaction/list.do"; // 菜单地址(权限用)

    @Autowired
    private TransactionDao transactionDao;

    @Autowired
    private InnerBillService billService;

    private Gson gson = new GsonBuilder().setPrettyPrinting().create();

    /**
     * 列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/list")
    public ModelAndView list(Page page) {

        logBefore(logger, "列表");

        logger.info("page:" + gson.toJson(page));

        ModelAndView mv = this.getModelAndView();
        PageData pd = this.getPageData();
        String owner = pd.get("owner") != null ? (String) pd.get("owner") : "";
        String searchKey = pd.get("searchKey") != null ? (String) pd.get("searchKey") : "";

        // 从session获取用户信息
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        List<TransactionEntity> list = transactionDao.listPageTransaction(page,
                user.getUSER_ID());

        List<TransactionEntityWrapper> listWrapper = list.stream().map(p -> new TransactionEntityWrapper(p)).collect(Collectors.toList());

        mv.addObject("varList", listWrapper);

        mv.setViewName("system/transaction/transaction_list");

        mv.addObject("owner", owner);
        mv.addObject("searchKey", searchKey);
        mv.addObject("page", page);
        mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限

        return mv;
    }

    /**
     * @return
     */
    public Map<String, String> getHC() {
        Subject currentUser = SecurityUtils.getSubject(); // shiro管理的session
        Session session = currentUser.getSession();
        return (Map<String, String>) session.getAttribute(Const.SESSION_QX);
    }


    /**
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format,
                true));
    }

}
